package com.qtong.rotatenavigation.adapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.qtong.rotatenavigation.R;
import com.qtong.rotatenavigation.R.id;
import com.qtong.rotatenavigation.R.layout;
import com.qtong.rotatenavigation.dao.SqLiteUtils;
import com.qtong.rotatenavigation.entity.IndexGrid;
import com.qtong.rotatenavigation.entity.XiangxiGrid;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class XiangxiAdapter extends BaseAdapter {

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		initlistgrid();
		super.notifyDataSetChanged();
	}
	private Context context;
	private AssetManager am;
	private IndexGrid indexgird;
	private List<XiangxiGrid> listgrid=new ArrayList<XiangxiGrid>();
	public XiangxiAdapter(Context context,IndexGrid indexgrid){
		this.context=context;
		this.indexgird=indexgrid;
		initlistgrid();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listgrid.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listgrid.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	class viewholder{
		ImageView iv;
		TextView tv;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		viewholder vh;
		System.out.println(convertView==null);
		if(convertView==null){
			convertView=LayoutInflater.from(context).inflate(R.layout.common_item, null);
			vh=new viewholder();
			vh.iv=(ImageView)convertView.findViewById(R.id.ItemImage);
			vh.tv=(TextView)convertView.findViewById(R.id.ItemText);
			convertView.setTag(vh);
		}else{
			vh=(viewholder)convertView.getTag();
		}
		vh.iv.setImageBitmap(listgrid.get(position).getBitmap());
		vh.tv.setText(listgrid.get(position).getName());
		
		return convertView;
	}
	public void initlistgrid(){
		listgrid.clear();
		am=context.getAssets();
		SqLiteUtils su=new SqLiteUtils(context);
		Cursor cursor=su.getReadableDatabase().rawQuery("select _id,name,image,uri,count,enddate,fid from xiangxi where fid=? order by level desc", new String[]{String.valueOf(indexgird.getId())});
		while(cursor.moveToNext()){
			XiangxiGrid xg=new XiangxiGrid();
			xg.setId(cursor.getInt(0));
			xg.setName(cursor.getString(1));
			xg.setImage(cursor.getString(2));
			xg.setUrl(cursor.getString(3));
			xg.setCount(cursor.getInt(4));
			xg.setFid(cursor.getInt(6));
			try {
				xg.setBitmap(BitmapFactory.decodeStream(am.open(xg.getImage())));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listgrid.add(xg);
		}
		cursor.close();
		su.close();
	}
	public void update(int id){
		SqLiteUtils su=new SqLiteUtils(context);
		SQLiteDatabase sd=su.getWritableDatabase();
		Cursor cursor= su.getReadableDatabase().rawQuery("select _id,fid,name,uri,image,count,enddate,level from xiangxi where _id=?", new String[]{String.valueOf(id)});
		if(cursor.moveToNext()){
			int count=cursor.getInt(cursor.getColumnIndex("count"));
			int level=cursor.getInt(cursor.getColumnIndex("level"));
			if(count%5==0&&count!=0){
				sd.execSQL("update xiangxi set count=count+1,enddate=datetime('now'),level=level+1 where _id=?", new Object[]{id});
			}else{
				sd.execSQL("update xiangxi set count=count+1,enddate=datetime('now') where _id=?", new Object[]{id});
			}
			Log.i("xiangxi", "id:"+id+"name:"+cursor.getString(cursor.getColumnIndex("name"))+"count:"+count+"level:"+level);
		}
		sd.close();
		su.close();
	}
	public void addCommon(XiangxiGrid xgrid){
		SqLiteUtils su=new SqLiteUtils(context);
		SQLiteDatabase sd=su.getWritableDatabase();
		sd.execSQL("insert into changyong(name,image,uri,count,enddate) values (?,?,?,?,datetime('now'))",new Object[]{xgrid.getName(),xgrid.getImage(),xgrid.getUrl(),xgrid.getCount()});
		sd.close();
		su.close();
	}
	public void delete(int id){
		SqLiteUtils su=new SqLiteUtils(context);
		SQLiteDatabase sd=su.getWritableDatabase();
		sd.execSQL("delete from xiangxi where _id=?", new Object[]{id});
		sd.close();
		su.close();
	}

}
